package com.finance.cooperate.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Charsets;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.*;

/**
 * @ClassName FileUtils
 * @Description 文件工具类
 * @Author shen
 * @Date 2022/10/14 18:41
 * @Modify ...
 */
public class FileUtils {

    /**
     * @Author shen
     * @Description 读取json文件，返回json对象
     * @Date 18:49 2022/10/14
     * @Param [path]
     * @return com.alibaba.fastjson.JSON
     **/
    public static JSONObject readJsonFile(String path) {


        ClassPathResource resource = new ClassPathResource(path);

        try {

            InputStream inputStream = resource.getInputStream();

            if (null != inputStream) {

                String jsonStr = IOUtils.toString(inputStream, Charsets.UTF_8.toString());

                return JSON.parseObject(jsonStr);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


}
